layui.use(['table', 'admin', 'ax','treetable','simTool'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var treetable = layui.treetable;
    var simTool = layui.simTool;

    /**
     * 商品分类表管理
     */
    var Category = {
        tableId: "categoryTable",
        condition: {
            categoryId: "",
            categoryName: "",
            level: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Category.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'categoryName', sort: true, title: '分类名称'},
            {field: 'icon', sort: true, title: '图标', templet:
                    function (d) {
                return '<div id="photo_big" class="photo_big"><img src="' +Feng.ctxPath+d.icon+'"  style="height: 50px;width: 50px;" /> <div>'
                },align:'center'},
            {field: 'weights', sort: true, title: '分类排序'},
            {field: 'level', sort: true, title: '级别 '},
            {field: 'parentId', sort: true, title: ' 父级id'},
            {field: 'isImportant',align:'center', sort: true, title: '首页显示',
                templet:'#showTpl'
            },
            {field: 'revision', sort: true, hide: true,title: '乐观锁'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'updateTime', sort: true,hide: true, title: '更新时间'},
            {field: 'recordStatus', sort: true,hide: true, title: '记录状态 默认：0已删除、1：正常'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击菜单树时
     */
    Category.onClickCategory = function (e, treeId, treeNode) {
        Category.condition.categoryId = treeNode.id;
        Category.search();
    };

    /**
     * 点击查询按钮
     */
    Category.search = function () {
        var queryData = {};
        queryData['categoryName'] = $("#categoryName").val();
        queryData['level'] = $("#level").val();
        queryData['isImportant'] = $("#isImportant").val();
        Category.initTable(queryData);
    };


    /**
     * 弹出添加对话框
     */
    Category.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加商品分类表',
            content: Feng.ctxPath + '/category/add',
            end: function () {
                admin.getTempData('formOk') &&Category.initTable();

            }
        });
    };

    /**
     * 导出excel按钮
     */
    Category.exportExcel = function () {
        var checkRows = table.checkStatus(Category.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Category.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改商品分类表',
            content: Feng.ctxPath + '/category/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') &&Category.initTable();

            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Category.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/category/delete", function (data) {
                Feng.success("删除成功!");
                Category.initTable();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    Category.initTable  = function( data ) {
           return  treetable.render({
                elem: '#' + Category.tableId,
                url: Feng.ctxPath + '/category/list',
                page: false,
                where:data,
                height: "full-158",
                cellMinWidth: 100,
                cols: Category.initColumn(),

                treeColIndex: 2,
                treeSpid: "0",
                treeIdName: 'id',
                treePidName: 'parentId',
                treeDefaultClose: true,
                treeLinkage: true
            });
        }


    // 渲染表格
    var tableResult = Category.initTable();

    $('#expandAll').click(function () {
        treetable.expandAll('#' + Category.tableId);
    });
    $('#foldAll').click(function () {
        treetable.foldAll('#' + Category.tableId);
    });



    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Category.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Category.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Category.exportExcel();
    });
    //按钮渲染
    var sim = new simTool("","");
    sim.updateButton("/category/editShowInPage",Category.tableId,"isImportant",[1,0])


    // 工具条点击事件
    table.on('tool(' + Category.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Category.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Category.onDeleteItem(data);
        }
    });
});
